package com.fick.tags;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.News;
import com.fick.util.General;

/**
 * 显示指定栏目信息
 * 
 * @author 王浩强 2009-08-14
 * 
 * **/
public class NewsTag extends TagSupport {
	private static Log log = LogFactory.getLog(NewsTag.class);
	// 栏目id
	private int channelid = 0;
	// 从第几条开始
	private int beginNum = 0;
	// 显示几条
	private int num = 10;
	// 标题长度
	private int length = 30;
	// table属性
	private String tableAttr = "";
	// tr属性
	private String trAttr = "";
	// dot td 属性
	private String dotTdAttr = "";
	// title td 属性
	private String titleTdAttr = "";
	// date td 属性
	private String dateTdAttr = "";
	// dot 图片
	private String dotSrc = "";
	// dot 图片属性
	private String dotAttr = "";
	// <a>标签href页面
	private String linkPage = "";
	// <a>标签属性
	private String linkAttr = "";
	// 是否显示日期
	private boolean isDate = true;
	// 日期格式
	private String dateFormat = "yyyy-MM-dd";
	// 是否显示子栏目
	private boolean isChild = false;

	// 子栏目标题属性
	private String clikTdAttr = "";
	private String spanAttr = "";

	// 是否显示点击量
	private boolean isCliknum = false;

	private boolean isSign = false; // 是否签收

	@Override
	public int doStartTag() throws JspException {

		try {
			StringBuffer sb = new StringBuffer();
			NewsDAO dao = new NewsDAO();
			List<News> list = null;
			if (isChild) {
				list = dao.findPageByHql(
						"where isAss='1' and channelid.channelId in(select channelId from Channel where fatherId=" + channelid
								+ ") order by addDate desc", 1, beginNum + num);
			} else {
				list = dao.findPageByHql("where isAss='1' and channelid.channelId=" + channelid + " order by addDate desc", 1,
						beginNum + num);
			}

			if (list != null && list.size() > beginNum) {
				News news;
				sb.append("<table ");
				sb.append(tableAttr);
				sb.append(" ><tbody>");
				for (int i = beginNum; i < list.size(); i++) {
					news = list.get(i);
					sb.append("<tr ");
					sb.append(trAttr);
					sb.append(" >");
					sb.append("<td ");
					sb.append(dotTdAttr);
					sb.append(" >");
					if (dotSrc.trim().length() > 0) {
						sb.append("<img src='");
						sb.append(dotSrc);
						sb.append("' ");
						sb.append(dotAttr);
						sb.append(" />");
					}
					sb.append("</td>");
					sb.append("<td ");
					sb.append(titleTdAttr);
					sb.append(" >");
					if (isChild) {
						sb.append("[<span ");
						sb.append(spanAttr);
						sb.append(">");
						sb.append("<a href=\"#\">" + news.getChannelid().getName());
						sb.append("</a></span>] ");
					}
					if (news.getFilePath() != null && news.getFilePath().trim().length() > 0) {
						sb.append("<a href='/downloadFile.jsp?filename=");
						sb.append(java.net.URLEncoder.encode(news.getFilePath(), "UTF-8"));
						sb.append("' >");
					} else {
						sb.append("<a href='");
						sb.append(linkPage);
						sb.append("?channelid=");
						sb.append(channelid);
						sb.append("&newsid=");
						sb.append(news.getNewId());
						sb.append("' ");
						sb.append(linkAttr);
						sb.append(" >");
					}
					sb.append(General.trimStringInLength(news.getTitle(), length));
					SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
					long day = 0;
					try {
						java.util.Date date = myFormatter.parse(myFormatter.format(new Date()));
						java.util.Date mydate = myFormatter.parse(myFormatter.format(news.getAddDate()));
						day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
					} catch (Exception er) {
						er.printStackTrace();
					}
					if (day <= 0) {
						sb.append("<img src='images/new.gif'>");
					}
					sb.append("</a>");
					sb.append("</td>");
					if (isCliknum) {
						sb.append("<td ");
						sb.append(clikTdAttr);
						sb.append(">");
						sb.append("[" + news.getClickNum() + "]");
						sb.append("</td>");
					}
					if (isDate) {
						sb.append("<td ");
						sb.append(dateTdAttr);
						sb.append(" >");
						SimpleDateFormat df = new SimpleDateFormat(dateFormat);
						if (day <= 1) {
							sb.append("<font color=\"red\">" + df.format(news.getAddDate()) + "</font>");
						} else {
							sb.append(df.format(news.getAddDate()));
						}
						sb.append("</td>");
					}
					sb.append("</tr>");
				}
				sb.append("</tbody></table>");
			}
			JspWriter out = pageContext.getOut();
			out.println(sb.toString());
			out.flush();
		} catch (IOException e) {
			log.error("标签显示错误" + e);
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public int getBeginNum() {
		return beginNum;
	}

	public int getChannelid() {
		return channelid;
	}

	public String getClikTdAttr() {
		return clikTdAttr;
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public String getDateTdAttr() {
		return dateTdAttr;
	}

	public String getDotAttr() {
		return dotAttr;
	}

	public String getDotSrc() {
		return dotSrc;
	}

	public String getDotTdAttr() {
		return dotTdAttr;
	}

	public boolean getIsChild() {
		return isChild;
	}

	public boolean getIsCliknum() {
		return isCliknum;
	}

	public boolean getIsDate() {
		return isDate;
	}

	public boolean getIsSign() {
		return isSign;
	}

	public int getLength() {
		return length;
	}

	public String getLinkAttr() {
		return linkAttr;
	}

	public String getLinkPage() {
		return linkPage;
	}

	public int getNum() {
		return num;
	}

	public String getSpanAttr() {
		return spanAttr;
	}

	public String getTableAttr() {
		return tableAttr;
	}

	public String getTitleTdAttr() {
		return titleTdAttr;
	}

	public String getTrAttr() {
		return trAttr;
	}

	public void setBeginNum(int beginNum) {
		this.beginNum = beginNum;
	}

	public void setChannelid(int channelid) {
		this.channelid = channelid;
	}

	public void setClikTdAttr(String clikTdAttr) {
		this.clikTdAttr = clikTdAttr;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public void setDateTdAttr(String dateTdAttr) {
		this.dateTdAttr = dateTdAttr;
	}

	public void setDotAttr(String dotAttr) {
		this.dotAttr = dotAttr;
	}

	public void setDotSrc(String dotSrc) {
		this.dotSrc = dotSrc;
	}

	public void setDotTdAttr(String dotTdAttr) {
		this.dotTdAttr = dotTdAttr;
	}

	public void setIsChild(boolean isChild) {
		this.isChild = isChild;
	}

	public void setIsCliknum(boolean isCliknum) {
		this.isCliknum = isCliknum;
	}

	public void setIsDate(boolean isDate) {
		this.isDate = isDate;
	}

	public void setIsSign(boolean isSign) {
		this.isSign = isSign;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public void setLinkAttr(String linkAttr) {
		this.linkAttr = linkAttr;
	}

	public void setLinkPage(String linkPage) {
		this.linkPage = linkPage;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public void setSpanAttr(String spanAttr) {
		this.spanAttr = spanAttr;
	}

	public void setTableAttr(String tableAttr) {
		this.tableAttr = tableAttr;
	}

	public void setTitleTdAttr(String titleTdAttr) {
		this.titleTdAttr = titleTdAttr;
	}

	public void setTrAttr(String trAttr) {
		this.trAttr = trAttr;
	}
}
